import java.util.Stack;

public class sort {
    //插入排序
    public static void insertSort(int[] array){
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if (array[j] > tmp) {
                    array[j+1] = array[j];
                } else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }
    //希尔排序
    public static void shellSort(int[] array){
        int gap = array.length;
        while (gap > 1) {
            gap = gap / 3 + 1;
            shell(array,gap);
        }
    }
    public static void shell(int[] array,int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0; j = j - gap) {
                if (array[j] > tmp) {
                    array[j+gap] = array[j];
                } else {
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }
    //选择排序
    public static void selectSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            for (int j = i + 1; j < array.length; j++) {
                if (array[i] > array[j]) {
                    int tmp = array[i];
                    array[i] = array[j];
                    array[j] = tmp;
                }
            }
        }
    }

// 堆排序
    public static void heapSort(int[] array){
        createHeap(array);
        for (int i = array.length - 1; i > 0 ; i--) {
            int tmp = array[i];
            array[i] = array[0];
            array[0] = tmp;
            siftDown(0,array,i);
        }
    }
    public static void createHeap(int[] array) {
        for (int i = (array.length-1-1)/2; i >= 0 ; i--) {
            siftDown(i,array,array.length);
        }
    }
    public static void siftDown(int parent,int[] array,int end) {
        int child = 2*parent+1;
        while (child < end) {
            if (child+1 < end && array[child] < array[child+1]) {
                child++;
            }
            if (array[parent] < array[child]) {
                int tmp = array[parent];
                array[parent] = array[child];
                array[child] = tmp;
            }
            parent = child;
            child = 2*parent+1;
        }
    }

    // 冒泡排序
    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length - 1; i++) {
            boolean T = true;
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j+1]) {
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    T = false;
                }
            }
            if (T) {
                break;
            }
        }
    }
    // 快排
    public static void quickSort(int[] array){
        quickSortfun(array,0,array.length-1);
    }
    public static void quickSortfun(int[] array,int left,int right){
        if (left>=right) {
            return;
        }
        int par = partition(array,left,right);
        quickSortfun(array,left,par-1);
        quickSortfun(array,par+1,right);
    }
    public static int partition(int[] array,int left,int right) {
        int tmp = left;
        while (left < right) {
            while (left < right && array[right] >= array[tmp]) {
                right--;
            }
            while (left < right && array[left] <= array[tmp]) {
                left++;
            }
            int var = array[left];
            array[left] = array[right];
            array[right] = var;
        }
        int var = array[tmp];
        array[tmp] = array[left];
        array[left] = var;
        return left;
    }
    //快排非递归
    public static void quickSortNor(int[] array){
        Stack<Integer> stack = new Stack<>();
        int left = 0;
        int right = array.length - 1;
        int par = partition(array,left,right);
        if (par > left + 1) {
            stack.push(left);
            stack.push(par-1);
        }
        if (par < right - 1) {
            stack.push(par+1);
            stack.push(right);
        }
        while (!stack.isEmpty()){
            right = stack.pop();
            left = stack.pop();
            par = partition(array,left,right);
            if (par > left + 1) {
                stack.push(left);
                stack.push(par-1);
            }
            if (par < right - 1) {
                stack.push(par+1);
                stack.push(right);
            }
        }
    }
    //归并排序
    public static void mergeSort(int[] array) {
        mergeSortFun(array,0,array.length-1);
    }
    public static void mergeSortFun(int[] array,int left,int right) {
        if (left >= right) {
            return;
        }
        int mid = (left + right) / 2;
        mergeSortFun(array,left,mid);
        mergeSortFun(array,mid+1,right);
        merge(array,left,right,mid);
    }
    public static void merge(int[] array,int left,int right,int mid) {
        int[] tmp = new int[right - left + 1];
        int s1 = left;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = right;
        int k = 0;
        while (s1 <= e1 && s2 <= e2) {
            if (array[s1] < array[s2]) {
                tmp[k] = array[s1];
                k++;
                s1++;
            }else {
                tmp[k] = array[s2];
                k++;
                s2++;
            }
        }
        while (s1 <= e1) {
            tmp[k++] = array[s1++];
        }
        while (s2 <= e2) {
            tmp[k++] = array[s2++];
        }
        for (int i = 0; i < k; i++) {
            array[i+left] = tmp[i];
        }
    }

}
